package com.shrios;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 不知道运用了什么东西，使得把其中的bean改变，授权的代码不改变
 */


@Configuration
public class ShrioConfig {

    @Bean(value = "delegatingFilterProxy")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setUnauthorizedUrl("/unAuth");


        Map<String, String> map = new HashMap<>();

        map.put("/img/**", "anon");//img
        map.put("/js/**", "anon");//js
        map.put("/css/**", "anon");//css

        map.put("/add", "authc");
        map.put("/update", "authc");
        map.put("/add", "perms[user:add]");


//        map.put("/update", "perms[user:update]");
//        map.put("/update", "perms[user:update]");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        return shiroFilterFactoryBean;
    }

    @Bean
    public SecurityManager defaultWebSecurityManager(UserRealm userRealm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager(userRealm);
        return defaultWebSecurityManager;
    }
}
